﻿using MediatR;
using MonentExam1013.Api.Read.Appliaction.Commond;
using MonentExam1013.Api.Read.Dto;
using MonentExam1013.Domain;
using MonentExam1013.ErrorCode;
using MonentExam1013.infrastrunceture;

namespace MonentExam1013.Api.Read.Appliaction.CommondHandler
{
    public class ShowUserCommondHandler : IRequestHandler<ShowUserCommond, ApiResult<ApiPaging<ShowUserDto>>>
    {
        private readonly IBaseIrepository<User> user;

        public ShowUserCommondHandler(IBaseIrepository<User> user)
        {
            this.user = user;
        }

        public Task<ApiResult<ApiPaging<ShowUserDto>>> Handle(ShowUserCommond request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<ShowUserDto>> result = new ApiResult<ApiPaging<ShowUserDto>>();
            result.Code=ApiEnum.查询成功;
            result.Msg=ApiEnum.查询成功.ToString();
            var list = from a in user.GetAll()
                       select new ShowUserDto
                       {
                           UserId = a.UserId,
                           UserName = a.UserName,
                           Name = a.Name,
                           Sex = a.Sex,
                           Phone = a.Phone,
                           Door = a.Door,
                           Jop = a.Jop,
                           Role = a.Role,
                           States=a.States,
                           Desc = a.Desc
                       };
            var totoCount = list.Count();
            var totoPage=(int)Math.Ceiling(totoCount*1.0/request.Pagesize);
            list=list.OrderBy(x=>x.UserId).Skip((request.Pageindex-1)*request.Pagesize).Take(request.Pagesize);
            ApiPaging<ShowUserDto> paging = new ApiPaging<ShowUserDto>();
            paging.totoPage = totoPage;
            paging.totoCount = totoCount;
            paging.data=list.ToList();
            result.data=paging;
            return Task.FromResult(result);
        }
    }
}
